package com.lemon.smartsafetynew.ui.adapter.record;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafetynew.entity.record.GroupMajorHazardvo;
import com.lemon.smartsafetynew.entity.record.MajorHazardVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;

/**
 * Created by JiangAn on 2018/5/28.
 */

public class MajorExpandaleAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<GroupMajorHazardvo> mDataList;

    public MajorExpandaleAdapter(Context context, List<GroupMajorHazardvo> mDataList) {
        this.context = context;
        this.mDataList = mDataList;
    }

    @Override
    public int getGroupCount() {
        return mDataList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mDataList.get(groupPosition).getMajorHazardVoList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mDataList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mDataList.get(groupPosition).getMajorHazardVoList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expand_group, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        if (isExpanded) {
            groupViewHolder.ivChangeView.setImageResource(R.mipmap.down_gray);
        } else {
            groupViewHolder.ivChangeView.setImageResource(R.mipmap.right_gray);
        }
        groupViewHolder.tvMajorPosition.setText(CommonUtil.avoidNullString(mDataList.get(groupPosition).getMajor_danger_name()));
        if(!TextUtils.isEmpty(mDataList.get(groupPosition).getChecked_regularly())){
            String status = mDataList.get(groupPosition).getChecked_regularly();
            groupViewHolder.tvStatus.setText(status);
            if(status.equals("是")){
                groupViewHolder.tvStatus.setTextColor(Color.parseColor("#60CC86"));
            }else if(status.equals("否")){
                groupViewHolder.tvStatus.setTextColor(Color.parseColor("#EA654F"));
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expand_child, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        MajorHazardVo vo = mDataList.get(groupPosition).getMajorHazardVoList().get(childPosition);
        childViewHolder.tvItemName.setText(CommonUtil.avoidNullString(vo.getMajor_danger_name()));
        childViewHolder.tvStoreThingCountLimit.setText(CommonUtil.avoidNullString(vo.getThreshold()));
        childViewHolder.tvStoreThingCount.setText(CommonUtil.avoidNullString(vo.getChemical_amount()));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupViewHolder {
        TextView tvMajorContent;
        TextView tvMajorPosition;
        TextView tvStatus;
        ImageView ivChangeView;

        public GroupViewHolder(View convertView) {
            tvMajorPosition = (TextView) convertView.findViewById(R.id.tv_major_position);
            ivChangeView = (ImageView) convertView.findViewById(R.id.iv_change_view);
            tvStatus = (TextView) convertView.findViewById(R.id.tv_status);
            tvMajorContent = (TextView) convertView.findViewById(R.id.tv_major_content);
        }
    }

    static class ChildViewHolder {
        TextView tvItemName;
        TextView tvStoreThingCountLimit;
        TextView tvStoreThingCount;
        public ChildViewHolder(View convertView) {
            tvItemName = (TextView) convertView.findViewById(R.id.tv_item_name);
            tvStoreThingCountLimit = (TextView) convertView.findViewById(R.id.tv_store_thing_count_limit);
            tvStoreThingCount = (TextView) convertView.findViewById(R.id.tv_store_thing_count);
        }
    }
}
